<?php

namespace LowCode;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use LowCode\Core\Admin;

class Provider extends ServiceProvider
{
    protected array $config = [];

    /**
     * 引导包服务
     */
    public function boot(): void
    {
        $this->publishes([getLowCodePath('config/lowcode.php') => config_path('lowcode.php')]);
    }

    public function register(): void
    {
        $this->mergeConfigFrom(Admin::getConfigPath("lowcode.php"), 'lowcode');
        $this->config = config('lowcode');
        $this->routes();
    }

    protected function routes(): void
    {
        $publicName = Admin::getConfig("public");
        Route::get($publicName, function () {
            return response(Admin::factory()->getWeb());
        });
        $prefix = trim($publicName,DIRECTORY_SEPARATOR)."-api";
        $name = $prefix.".";
        $namespace = null;
        $middlewares = [];
        Route::prefix($prefix)->name($name)->middleware($middlewares)->group(function (){
            Route::get("crud/{model}/{action}", "LowCode\Controllers\CrudController@index");
            Route::fallback(function () {
                // ...
            });
        });
    }
}
